<?php

namespace App\Console\Commands;

use App\Model\buy;
use App\Model\ssq;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class sendWin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sendWin';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $res = (new ssq())->getLastQh();
        $qihao = $res->qihao;
        $number = $res->number;

        $winResObj = (new buy())->getLastQh($qihao);

        $len = count($winResObj);

        if($len > 0){
            foreach ($winResObj as $buyObj) {
                $buyArr = $buyObj->toArray();
                if($buyArr['no1']){
                    $reward = $this->compareSsq($buyArr['no1'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no2']){
                    $reward = $this->compareSsq($buyArr['no2'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no3']){
                    $reward = $this->compareSsq($buyArr['no3'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no4']){
                    $reward = $this->compareSsq($buyArr['no4'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no5']){
                    $reward = $this->compareSsq($buyArr['no5'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }
            }
        }
        if($res->flag != 1){
            ssq::where(['qihao'=>$qihao])->update(['flag'=>1]);
        }
    }

    private function compareSsq($str1,$str2){
        $arr1 = explode('|',$str1);
        $arr2 = explode('|',$str2);

        $blue1 =  array_pop($arr1);
        $blue2 =  array_pop($arr2);

        //数组比对，返回相同元素到新数组
        $newArr = array_intersect($arr1,$arr2);


        $num = count($newArr);

        $lotteryNum = 0;

        switch ($num) {
            case 1:
                $lotteryNum = 1;
                break;
            case 2:
                $lotteryNum = 2;
                break;
            case 3:
                $lotteryNum = 3;
                break;
            case 4:
                $lotteryNum = 4;
                break;
            case 5:
                $lotteryNum = 5;
                break;
            case 6:
                $lotteryNum = 6;
                break;
        }

        $reward = 0;
        if($blue1 == $blue2){
            if($lotteryNum == 1 || $lotteryNum == 2 || $lotteryNum == 0){
                $reward = 6;
            }
            if($lotteryNum == 3){
                $reward = 5;
            }
            if($lotteryNum == 4){
                $reward = 4;
            }
            if($lotteryNum == 5){
                $reward = 3;
            }if($lotteryNum == 6){
                $reward = 1;
            }
        }else{
            if($lotteryNum == 4){
                $reward = 5;
            }
            if($lotteryNum == 5){
                $reward = 4;
            }
            if($lotteryNum == 6){
                $reward = 2;
            }
        }

        return $reward;
    }

    private function sendMail($reward){
        $res = (new ssq())->getLastQh();
        $flag = $res->flag;
        if($flag == 0){
            if($reward == 1){
                for ($i=0; $i < 100; $i++) {
                    //发邮件通知
                    Mail::raw('牛逼了，无敌了！！！！！！！！！！！', function($message){
                        $message->to('171161005@qq.com');
                        $message->subject('牛逼了，无敌了!!!!!!!!!!!!!!!!!!');
                    });
                }
            }elseif ($reward == 2) {
                for ($i=0; $i < 3; $i++) {
                    //发邮件通知
                    Mail::raw('草！就差1下！', function($message){
                        $message->to('171161005@qq.com');
                        $message->subject('草啊!就差1下下!');
                    });
                }
            }elseif ($reward == 3) {
                //发邮件通知
                Mail::raw('3', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('3,15000');
                });
            }elseif ($reward == 4) {
                //发邮件通知
                Mail::raw('4', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('4,1000');
                });
            }elseif ($reward == 5) {
                //发邮件通知
                Mail::raw('5', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('下次免费');
                });
            }else{
                //发邮件通知
                Mail::raw('优惠一半', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('优惠一半');
                });
            }
        }
    }
}

